#!/usr/local/bin/python
# Rob Dennis - Lurchington Q. Delacroix
# Keasby - TV renamer
# Project Homepage: http://code.google.com/p/keasby/

#---Python modules
import os
import unittest
import time
import logging
logging.basicConfig(level=logging.DEBUG, format="%(levelname)s: %(message)s")
from os.path import join as pjoin
from os.path import exists as pexists
from string import Template
#---Keasby Modules
from base_classes import KeasbyTestCase
#---Utility
from keasby_toolbox import reshape_dict

#--- keasby_emcee

class KeasbyEmceeTestCase(KeasbyTestCase):
    """
    Tests the interactions used in KeasbyEmcee. The class tasked with
    tying together many of the functions of Keasby.
    """

    def setUp(self):
        """
        Instantiates the KeasbyEmcee instance
        """

        self.mc = keasby_emcee.KeasbyEmcee()

    def test_sanity(self):
        """
        Basic test of keasby_emcee functions
        """

        self.assertLog(self.mc,"Invalid KeasbyEmcee instance: %s"%self.mc)

    def test_option_tuples(self):
        """
        Tests the return values of the options, suitable for displaying
        in a gui
        """

        output_params = ("label","tooltip","value")
        options_mapping = self.mc.get_options_mapping()

        self.assertLog(options_mapping)
        self.assertLog(len(output_params) == len(options_mapping))
        for opt_label in options_mapping.iterkeys():
            self.assertLog(opt_label in output_params)


class KeasbyEmceeInternalTestCase(KeasbyTestCase):
    """
    Tests the interactions used in KeasbyEmcee. The class tasked with
    tying together many of the functions of Keasby.
    """

    def setUp(self):
        """
        Instantiates the KeasbyEmcee instance
        """

        self.mc = keasby_emcee.KeasbyEmcee()

    def test_sanity(self):
        """
        Basic test of keasby_emcee functions
        """

        self.assertLog(self.mc,"Invalid KeasbyEmcee instance: %s"%self.mc)

#--- keasby_repo

class KeasbyRepoTestCase(KeasbyTestCase):
    """
    Tests the correct interaction with a Keasby repository instance
    """

    def setUp(self):
        """
        Instantiates a KeasbyInfoRepo instance
        """

        self.r = keasby_repo.KeasbyInfoRepo()

    def test_sanity(self):
        """
        basic test here
        """

        self.assertLog(self.r)

    def test_iterable(self):
        """
        tests the ability of KeasbyInfoRepo to act as an iterable
        """

        for source in self.r:
            self.assertLog(source)


class KeasbyRepoInternalTestCase(KeasbyTestCase):
    """
    Tests the inner workins of the KeasbyRepoClass, including the
    interaction with the KeasbySource class
    """

    def setUp(self):
        """
        Instantiates a KeasbyInfoRepo instance
        """

        self.r = keasby_repo.KeasbyInfoRepo()

    def test_sanity(self):
        """
        basic test here
        """

        self.assertLog(self.r)

#--- keasby_options

class KeasbyOptionsTestCase(KeasbyTestCase):
    """
    Tests that the information derived from the configuration management
    class is correct
    """

    def setUp(self):
        """
        Instantiates a KeasbyOptions instance
        """

        self.o = keasby_options.KeasbyOptions()

    def test_parse_args(self):
        """
        Tests for the validity of the two class attributes holding the
        command line (positional) arguments and command line options
        """

        self.assertLog(self.o.opts(),
                        "parsed options are invalid: %s"%self.o.opts,
                        self.o.opts())

    def test_cl_opts(self):
        """
        Tests for the validity of returning all argments that were specified
        on the command line, versus a defaulted or configuration file option
        """

        self.assertLog(self.o.cl_opts(),
                        "command line options are invalid",
                        self.o.cl_opts())

    def test_file_opts(self):
        """
        Tests for the validity of returning all argments that were specified
        on the configuration options, versus a defaulted or command-line option
        """

        self.assertLog(self.o.file_opts(),
                        "config file options are invalid",
                        self.o.file_opts())

    def test_default_opts(self):
        """
        Tests for the validity of returning all argments that were defaulted,
        as opposed to specified on the command line or a configuration file
        """

        self.assertLog(self.o.default_opts(),
                        "default options are invalid",
                        self.o.default_opts())

    def test_reparse_args(self):
        """
        tests the ability of the options parser to handle a change in options.
        The format for changing options can be a new string that will emulate
        the entered command line string. This new string will modify existing
        options, not reset any options not expressly modified by that string.
        """

        orig_opts = self.o.opts()


class KeasbyOptionsInternalTestCase(KeasbyTestCase):
    """
    Tests the internal workings of the KeasbyOptions class, including
    the interaction with config files
    """

    def setUp(self):
        """
        Instantiates a KeasbyOptions instance
        """

        self.o = keasby_options.KeasbyOptions()

    def test_sanity(self):
        """
        basic test here
        """

        self.assertLog(self.o)

    def test_parser_instance(self):
        """
        Tests for the validity of the parser attribute
        """

        self.assertLog(self.o.parser, "parser is invalid")

#--- Main

if __name__ == "__main__":

    unittest_list = []
##    unittest_list.append(MediaInterpreterTestCase)
##    unittest_list.append(KeasbyCacheTestCase)
##    unittest_list.append(KeasbyCacheLoadingTestCase)
##    unittest_list.append(KeasbyRepoTestCase)
##    unittest_list.append(KeasbyOptionsTestCase)
##    unittest_list.append(TvEpisodeTestCase)
    unittest_list.append(TvDbApiTestCase)

    for unittest_class in unittest_list:
        suite = unittest.TestLoader().loadTestsFromTestCase(unittest_class)
        unittest.TextTestRunner(verbosity=2).run(suite)

    internal_unittest_list = []
##    internal_unittest_list.append(MediaInterpreterInternalTestCase)
##    internal_unittest_list.append(KeasbyRepoInternalTestCase)
##    internal_unittest_list.append(KeasbyOptionsInternalTestCase)
##    internal_unittest_list.append(TvEpisodeInternalTestCase)
##    internal_unittest_list.append(TvDbApiInternalTestCase)

    for unittest_class in internal_unittest_list:
        suite = unittest.TestLoader().loadTestsFromTestCase(unittest_class)
        unittest.TextTestRunner(verbosity=2).run(suite)
